package action.notice;

import java.util.ArrayList;
import java.util.List;

import model.Notice;
import model.Page;
import model.User;

import DAO.NoticeDAO;
import DAO.DAOImple.NoticeImple;

import com.opensymphony.xwork2.ActionSupport;

public class NoticeAction extends ActionSupport {


	List<Notice> noticeList = new ArrayList<Notice>();

	private Notice notice;
	private User user;
	private NoticeImple noticeImple = new NoticeImple();
	private Page page;


	public String history() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Notice where state = 0", page.getCurrentPage(), 5);
		NoticeDAO noticeDao = new NoticeImple();
		noticeList = noticeDao.getNotice(page);
		/*
		System.out.println(noticeList.size());
		System.out.println("current:" + page.getCurrentPage());
		System.out.println("PageCount:" + page.getPageCount());
		System.out.println("PageSize:" + page.getPageSize());
		System.out.println("StartRow:" + page.getStartRow());
		*/
		return SUCCESS;
	}
	

	public String lastest() {

		NoticeDAO noticeDao = new NoticeImple();
		notice = noticeDao.getLastNotice();

		return SUCCESS;
	}

	public String publish() {
		
//		System.out.println(notice.getPublishTime());
		NoticeDAO noticeDao = new NoticeImple();
		noticeDao.addNotice(notice,user);

		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Notice where state = 0", page.getCurrentPage(), 5);
		noticeList = noticeDao.getNotice(page);
		
		return SUCCESS;
	}
	
	public String getNoticeInfo() {
		
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Notice where state = 0", page.getCurrentPage(), 5);
		
		NoticeDAO noticeDao = new NoticeImple();
System.out.println("sdgasgdgdhdh");
System.out.println(notice.getId());
		notice = noticeDao.getNoticeInfo(notice.getId());
		
		return SUCCESS;
	}

	public String delete() {

		NoticeDAO noticeDao = new NoticeImple();
		noticeDao.deleteNotice(notice.getId());
		
		page = new Page();
		page.setCurrentPage(1);
		page = page.getPage("T_Notice where state = 0", page.getCurrentPage(), 5);
		noticeList = noticeDao.getNotice(page);

		return SUCCESS;
	}
	
	public String update() {
		NoticeDAO noticeDao = new NoticeImple();
		noticeDao.updateNotice(notice);
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_Notice where state = 0", page.getCurrentPage(), 5);
		noticeList = noticeDao.getNotice(page);
		
		return SUCCESS;
	}

	
	public String execute() throws Exception {

		return SUCCESS;
	}
	

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public List<Notice> getNoticeList() {
		return noticeList;
	}

	public void setNoticeList(List<Notice> noticeList) {
		this.noticeList = noticeList;
	}
	
	public NoticeImple getNoticeImple() {
		return noticeImple;
	}

	public void setNoticeImple(NoticeImple noticeImple) {
		this.noticeImple = noticeImple;
	}

	


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	public Page getPage() {
		return page;
	}


	public void setPage(Page page) {
		this.page = page;
	}




	

	
}
